/*
 * @Copyright: 2023 - 2023 www.hoperun.com Inc. All rights reserved.
 */

package com.lifeservice.wxapi;

import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.core.domain.entity.SysDictData;
import com.lifeservice.common.utils.StringUtils;
import com.lifeservice.console.domain.LifeBanner;
import com.lifeservice.console.domain.LifeService;
import com.lifeservice.console.domain.appvo.AppBaseQueryVo;
import com.lifeservice.console.domain.appvo.AppHomePage;
import com.lifeservice.console.domain.appvo.AppServiceInfo;
import com.lifeservice.console.domain.appvo.AppTopBanner;
import com.lifeservice.console.mapper.LifeBannerMapper;
import com.lifeservice.console.mapper.LifeServiceMapper;
import com.lifeservice.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liu_yifeng
 * @function 首页服务查询接口
 * @date 2023/2/27 15:08
 */
@RestController
@RequestMapping("/wx/homepage")
public class WxServiceController {

    @Autowired
    private LifeServiceMapper lifeServiceMapper;

    @Autowired
    private LifeBannerMapper lifeBannerMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    @PostMapping("/info")
    public AjaxResult info(@RequestBody AppBaseQueryVo appBaseQueryVo) {
        // 首页对象
        AppHomePage homePage = new AppHomePage();
        List<SysDictData> dictDataList = iSysDictTypeService.selectDictDataByType("lifeservice_serviceclass");
        if (StringUtils.isNotEmpty(dictDataList)) {
            // 便捷服务列表
            Map<Long, List<AppServiceInfo>> serviceInfoMap = new LinkedHashMap<>();
            dictDataList = dictDataList.stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList());
            dictDataList.stream().forEach(dictData -> {
                String serviceClass = dictData.getDictValue();
                // 便捷服务列表
                List<AppServiceInfo> appServiceList = new LinkedList<>();
                LifeService queryService = new LifeService();
                queryService.setServiceClass(Integer.valueOf(serviceClass));
                queryService.setIsHide(0);
                List<LifeService> serviceListInDb = lifeServiceMapper.selectLifeServiceList(queryService);
                if (StringUtils.isNotEmpty(serviceListInDb)) {
                    serviceListInDb.stream().forEach(dbData -> {
                        AppServiceInfo appServiceInfo = new AppServiceInfo();
                        appServiceInfo.setServiceName(dbData.getServiceName());
                        appServiceInfo.setServiceIcon(dbData.getServiceIcon());
                        appServiceInfo.setServiceId(dbData.getId());
                        appServiceInfo.setPriority(dbData.getPriority());
                        appServiceList.add(appServiceInfo);
                    });
                    serviceInfoMap.put(dictData.getDictCode(), appServiceList);
                }
            });
            homePage.setServiceClassList(dictDataList);
            homePage.setServiceInfoMap(serviceInfoMap);
        }
        // 首页顶部banner列表
        List<AppTopBanner> topBannerList = new LinkedList<>();
        LifeBanner queryBanner = new LifeBanner();
        List<LifeBanner> bannerListInDb = lifeBannerMapper.selectLifeBannerList(queryBanner);
        if (StringUtils.isNotEmpty(bannerListInDb)) {
            bannerListInDb.stream().forEach(bannerInDb -> {
                AppTopBanner topBanner = new AppTopBanner();
                topBanner.setBannerPic(bannerInDb.getBannerPic());
                topBanner.setClickUrl(bannerInDb.getClickUrl());
                topBanner.setBannerType(bannerInDb.getBannerType());
                topBanner.setTag(bannerInDb.getTag());
                topBanner.setPriority(bannerInDb.getPriority());
                topBannerList.add(topBanner);
            });
            homePage.setTopBannerList(topBannerList.stream().sorted(Comparator.comparing(AppTopBanner::getPriority)).collect(Collectors.toList()));
        }
        return AjaxResult.success(homePage);
    }
}
